package com.cloudptt.wp.ui.views;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.GCallEventAdapter;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.listen.IDiaNumGet;
import com.cloudptt.wp.ui.views.listener.CustomListener;
import com.cloudptt.wp.ui.views.listener.DialListener;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View.*;


public class PTTLayout extends LinearLayout implements OnClickListener, OnTouchListener{

	public static final String TAG = "PTTLayout";
	private Context context = null;
	//	private DialLayout mDialLayout = null;
	private LinearLayout empty_ll;
	private LinearLayout mPttLayout;
	private ImageView mPttIv;
	private AnimationDrawable animation = null;
	//	private ImageView mAcceptIv;
//	private ImageView mDiaIv;
//	private ImageView mGroupIv;
//	private ImageView mGcallVideoIv;
//	private ImageView mHangupIv;
	private boolean isShowDia = false;

//	private AnimationDrawable animationDrawable;

	private GCallEventAdapter mCallEventAdapter = null;

	private CustomListener customListener;

	public void setDiaNumGet(IDiaNumGet diaNumGet) {
		this.diaNumGet = diaNumGet;
	}

	private IDiaNumGet diaNumGet = null;

	public PTTLayout(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public PTTLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public void setDialListener(DialListener listener) {
//		if(mDialLayout != null)
//		{
//			mDialLayout.setDialListener(listener);
//		}
	}

	public PTTLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		this.context = context;
		Log.d(TAG, "PTTLayout new");

		LayoutInflater.from(context).inflate(R.layout.ptt_layout, this);
//		mDialLayout = (DialLayout)findViewById(R.id.diaLy);
//		mDialLayout.setVisibility(View.GONE);
		empty_ll = (LinearLayout)findViewById(R.id.empty_ll);
		mPttLayout = (LinearLayout)findViewById(R.id.ptt_layout);
//		mDiaIv = (ImageView)findViewById(R.id.diaIv);
//		mGroupIv = (ImageView)findViewById(R.id.groupIv);
//		mWarningImageView = (ImageView)findViewById(R.id.warning_imageview);
		mPttIv = (ImageView)findViewById(R.id.pttIv);
//		mGcallVideoIv = (ImageView)findViewById(R.id.gcallVideoIv);
//		mAcceptIv = (ImageView)findViewById(R.id.acceptIv);
//		mHangupIv = (ImageView)findViewById(R.id.hangupIv);
//		mHangupIv.setVisibility(View.GONE);
	}

	public void setCallAdapter(GCallEventAdapter callEventAdapter){
		Log.d(TAG, "setCallAdapter");
		mCallEventAdapter = callEventAdapter;

		onResume();
	}
	public void updateUiForCallState(int callState)
	{
		Log.d(TAG, "updateUi callState = " + callState);
		if(callState == CallConst.CallState_InitState) {
			stopAnim();
			mPttIv.setImageResource(R.drawable.icon_group_ptt);
			mPttIv.setEnabled(true);
		}
	}
	public void updateUiForFloorState(int speakState,boolean isIdle)
	{
		if(mPttIv != null){
			//mPttImageView.setPressed(true);
			Log.d(TAG, "updateUi speakState = " + speakState + " isIdle = " + isIdle);
			if(speakState == CallConst.FloorState_NoPermission) {
				if(!isIdle)
				{
					mPttIv.setImageResource(R.drawable.icon_group_ptt_listen);
//					mPttIv.setEnabled(false);
				}
				else
				{
					stopAnim();
					mPttIv.setImageResource(R.drawable.icon_group_ptt);
//					mPttIv.setEnabled(true);
				}

			}
			else if(speakState == CallConst.FloorState_Permission)
			{
				showPttFrameAnimByXml();
//				mPttIv.setImageResource(R.drawable.icon_group_ptt_pressed);
//				mPttIv.setEnabled(true);
			}
			else
			{
				stopAnim();
				mPttIv.setImageResource(R.drawable.icon_group_ptt);
//				mPttIv.setEnabled(true);
			}
		}
	}



	private void showPttFrameAnimByXml() {
		stopAnim();
		// 设置图像视图的图像来源为帧动画的XML定义文件
		mPttIv.setImageResource(R.drawable.animation_list_ptt);
		mPttIv.setScaleType(ImageView.ScaleType.CENTER_CROP);
		// 从图像视图对象中获取帧动画
		animation = (AnimationDrawable) mPttIv.getDrawable();
		animation.start(); // 开始播放帧动画
	}
	public void stopAnim()
	{
		if(animation != null)
		{
			animation.stop();
		}
		mPttIv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
	}

	private void updatePttUI(){
		if(mPttIv != null){
			//mPttImageView.setPressed(true);
			mPttIv.setImageResource(R.drawable.icon_group_ptt_pressed);
		}

//		if(mPttImageView != null){
//			animationDrawable = (AnimationDrawable)mPttImageView.getDrawable();
//			animationDrawable.start();
//		}
//						showCallStatusTv(true);
		if(diaNumGet != null) {
			String numStr = diaNumGet.getDiaNum();
			String nameStr = numStr;
			boolean isEmergency = false;
			if (numStr != null
					&& numStr.length() > 0) {
				if (numStr.length() > 30) {
					CommDlg.showToast(context, R.string.err_num_toolong);
					return;
				}
				int type = AddrInfo.getUserTypeByNum(numStr);
				Log.d(TAG, "updatePttUI type = " + type);
				if(type == AddrInfo.GroupType_Crsc)
				{
					if (numStr.endsWith("299")) {
						isEmergency = true;
					}
					if (numStr.length() == 3) {
						numStr = "50" + numStr;
						nameStr = nameStr;
					}
					SysApplication app = ((SysApplication) context.getApplicationContext());
					Product product = app.getProduct();
					AddrInfo groupInfo = new AddrInfo(AddrInfo.GroupType_Crsc, nameStr, numStr, 0);
					product.setCurGroupInfo(groupInfo);
				}
				else if(type == AddrInfo.GroupType_Normal)
				{
					SysApplication app = ((SysApplication) context.getApplicationContext());
					Product product = app.getProduct();
					AddrInfo groupInfo = product.getGroupInfoByNum(numStr);
					product.setCurGroupInfo(groupInfo);
				}
				else
				{
					SysApplication app = ((SysApplication) context.getApplicationContext());
					Product product = app.getProduct();
					AddrInfo groupInfo = new AddrInfo(AddrInfo.GroupType_Normal, nameStr, numStr, 0);
					product.setCurGroupInfo(groupInfo);
				}
			}
			if (mCallEventAdapter != null)
			{
				mCallEventAdapter.onPtt(Const.MSG_TALK_ON, true, false, true, isEmergency);
			}
		}
	}
	public void onStop()
	{
		Log.d(TAG, "onStop");
//		mDiaIv.setOnClickListener(null);
//		mGcallVideoIv.setOnClickListener(null);
//		mGroupIv.setOnClickListener(null);
//		if(mPttLayout != null) {
//			mPttLayout.setOnTouchListener(null);
//		}
		if(mPttIv != null) {
			mPttIv.setOnTouchListener(null);
		}
//		mHangupIv.setOnClickListener(null);
//		if(mAcceptIv != null) {
//			mAcceptIv.setOnClickListener(null);
//		}
	}
	public void onResume()
	{
		Log.d(TAG, "onResume");
//		mDiaIv.setOnClickListener(this);
//		mGcallVideoIv.setOnClickListener(this);
//		mGroupIv.setOnClickListener(this);
//		if(mPttLayout != null) {
//			mPttLayout.setOnTouchListener(this);
//		}
		if(mPttIv != null) {
			mPttIv.setOnTouchListener(this);
		}
//		mHangupIv.setOnClickListener(this);
//		if(mAcceptIv != null) {
//			mAcceptIv.setOnClickListener(this);
//		}
	}

	private void doPttOff(boolean isMust){
		Log.d(TAG, "doPttOff");
//		if(animationDrawable != null){
//			animationDrawable.stop();
//		}
		if(mPttIv != null){
			mPttIv.setPressed(false);
			mPttIv.setImageResource(R.drawable.icon_group_ptt);
		}
		//				showCallStatusTv(false);
		if(mCallEventAdapter != null){
			mCallEventAdapter.onPtt(Const.MSG_TALK_OFF, true,false,true,false);
		}
	}

	private void pressPTT(boolean usingPtt){
		if(usingPtt){
			updatePttUI();
		}
	}

	public void setCustomView(int resId, CustomListener listener){
//		mDiaIv.setImageResource(resId);
		customListener = listener;
	}

//	public void showAcceptBtn()
//	{
//		mAcceptIv.setVisibility(View.VISIBLE);
//	}

	public void showCallBtn(boolean isShow){
//		Log.d(TAG, "showCallBtn isShow = " + isShow);
//		if(isShow){
//			if(mHangupIv != null
//					&& !mHangupIv.isShown()){
//				if(mCallEventAdapter.isEmergencyMt())
//				{
//					mHangupIv.setVisibility(View.GONE);
//				}
//				else
//				{
//					mHangupIv.setVisibility(View.VISIBLE);
//				}
//			}
//			if(mGroupIv != null
//					&& !mGroupIv.isShown()){
//				mGroupIv.setVisibility(View.VISIBLE);
//			}
//			if(mDiaIv != null
//					&& mDiaIv.isShown()){
//				mDiaIv.setVisibility(View.GONE);
//			}
//			if(mGcallVideoIv != null
//					&& mGcallVideoIv.isShown()){
//				mGcallVideoIv.setVisibility(View.GONE);
//			}
//		}else{
//			if(mHangupIv != null
//					&& mHangupIv.isShown()){
//				mHangupIv.setVisibility(View.GONE);
//			}
//			if(mGroupIv != null
//					&& mGroupIv.isShown()){
//				mGroupIv.setVisibility(View.GONE);
//			}
//			if(mDiaIv != null
//					&& !mDiaIv.isShown()){
//				mDiaIv.setVisibility(View.VISIBLE);
//			}
//			if(mGcallVideoIv != null
//					&& !mGcallVideoIv.isShown()){
//				mGcallVideoIv.setVisibility(View.VISIBLE);
//			}
//		}
	}

	public void clearState(){
		Log.d(TAG, "clearState");
//		showCallBtn(false);
		if(mPttIv != null){
			//mPttImageView.setPressed(true);
			mPttIv.setImageResource(R.drawable.icon_group_ptt);
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
//		if(R.id.hangupIv == view.getId()){
//			mAcceptIv.setVisibility(View.GONE);
//			if(mCallEventAdapter != null){
//				mCallEventAdapter.hangUpGroupCall();
//			}
//		}
//		else
//			if(R.id.acceptIv == view.getId()){
//			mAcceptIv.setVisibility(View.GONE);
//			if(mCallEventAdapter != null){
//				mCallEventAdapter.acceptGroupCall();
//			}
//		}
//		else if(R.id.gcallVideoIv == view.getId()){
//			if(mCallEventAdapter != null){
//				mCallEventAdapter.onPtt(Const.MSG_TALK_ON, true,true,true,false);
//			}
//		}
//		else if(R.id.diaIv == view.getId()){
//			Log.d(TAG, "click custom");
//			isShowDia = !isShowDia;
//			mDialLayout.clear();
//			if(isShowDia) {
//				empty_ll.setBackgroundColor(Color.WHITE);
//				mDiaIv.setImageResource(R.drawable.btn_dia_pressed);
//				mDialLayout.setVisibility(View.VISIBLE);
//			}
//			else {
//				empty_ll.setBackgroundColor(Color.parseColor("#DAE4EA"));
//				mDiaIv.setImageResource(R.drawable.btn_dia_normal);
//				mDialLayout.clear();
//				mDialLayout.setVisibility(View.GONE);
//			}
//			if(customListener != null)
//			{
//				customListener.customFunc();
//			}
//		}
//		else if(R.id.groupIv == view.getId()){
//			AddrInfo peerAddrInfo = mCallEventAdapter.getCurGroupInfo();
//			if(peerAddrInfo != null) {
//				Intent intent = new Intent();
//				intent.setClass(context, GroupMemberActivity.class);
//				intent.getBooleanExtra(GroupMemberActivity.KEY_SHOW, true);
//				intent.putExtra(SessionActivity.KEY, peerAddrInfo);
//				context.startActivity(intent);
//			}
//		}

	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d(TAG, "event action = "+event.getAction());
		if(view.getId() == R.id.pttIv){
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				pressPTT(true);
				return true;
			}else if(event.getAction() == MotionEvent.ACTION_UP){
				doPttOff(false);
				return true;
			}
		}
		return false;
	}

}
